import java.util.Arrays;

/**
 * 插入排序
 *
 * @Author: wanqian
 * @Date: 2021/2/1 1:19
 */
public class InsertSort {
    public static int[] arr = {4, 3, 65, 12, 1, -6, 46, 64};

    public static void main(String[] args) {
//        int[] arr = new int[100000];
//        for (int i = 0; i < 100000; i++) {
//            arr[i] = (int) (Math.random() * 10000);
//        }
        System.out.println("排序前" + Arrays.toString(arr));
        long start = System.currentTimeMillis();
        insertSort(arr);
        long end = System.currentTimeMillis();
        long consume = (end - start);
        System.out.println("耗时" + consume + "毫秒");
        System.out.println("排序后" + Arrays.toString(arr));
    }

    public static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int index = i-1;
            int value = arr[i];
            while (index >= 0 && arr[index] > value) {
                arr[index + 1] = arr[index];
                index--;
            }
            arr[index + 1] = value;
        }
    }
}
